{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 多进程通信与管理笔记\n",
    "\n",
    "本笔记涵盖了 Python 中的多进程通信与管理相关的函数及方法，包括 `Pipe`、`Process`、`send`、`recv`、`close`、`start` 以及 `join` 的基本使用方法。"
   ],
   "id": "46c47d1f0cebed9b"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Pipe 函数\n",
    "\n",
    "`Pipe()` 用于创建一个管道，实现两个进程之间的数据通信。该函数返回两个连接对象，通常分别用于发送和接收数据。"
   ],
   "id": "10e34e06f026e769"
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T07:38:17.411780Z",
     "start_time": "2025-03-04T07:38:17.382413Z"
    }
   },
   "source": [
    "from multiprocessing import Pipe\n",
    "\n",
    "# 创建管道，返回两个连接对象：conn1 和 conn2\n",
    "conn1, conn2 = Pipe()\n",
    "\n",
    "# 通过conn1发送数据\n",
    "conn1.send(\"Hello from conn1!\")\n",
    "\n",
    "# 通过conn2接收数据，并打印输出\n",
    "print(conn2.recv())  # 输出: Hello from conn1!"
   ],
   "id": "c42e0fee4d73f05b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello from conn1!\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Process 类\n",
    "\n",
    "`Process` 类用于创建新的进程，通过指定目标函数和参数来定义子进程的任务。"
   ],
   "id": "7a0d8be266e144b6"
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T07:46:46.363417Z",
     "start_time": "2025-03-04T07:46:46.278854Z"
    }
   },
   "source": [
    "from multiprocessing import Process\n",
    "\n",
    "# 定义子进程执行的函数\n",
    "def worker():\n",
    "    print(\"Worker process is running!\")\n",
    "\n",
    "# 创建进程对象\n",
    "if __name__ == '__main__':  # 在Windows下运行必须加入这一行\n",
    "    p = Process(target=worker)\n",
    "    p.start()  # 启动子进程\n",
    "    p.join()   # 等待子进程结束\n",
    "    print(\"Worker process has finished!\")"
   ],
   "id": "3f8b2cc2db30a6a3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Worker process has finished!\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. send()、recv() 与 close() 方法\n",
    "\n",
    "这些方法属于管道连接对象：\n",
    "- `send()` 用于发送数据；\n",
    "- `recv()` 用于接收数据；\n",
    "- `close()` 用于关闭连接，释放资源。"
   ],
   "id": "7eda86ca464ba153"
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T07:51:50.018027Z",
     "start_time": "2025-03-04T07:51:50.003914Z"
    }
   },
   "source": [
    "from multiprocessing import Pipe\n",
    "\n",
    "# 创建管道\n",
    "parent_conn, child_conn = Pipe()\n",
    "\n",
    "# 使用 child_conn 发送数据，然后关闭连接\n",
    "child_conn.send(\"Test message for close\")\n",
    "child_conn.close()\n",
    "\n",
    "# 使用 parent_conn 接收数据，并打印输出\n",
    "print(parent_conn.recv())  # 输出: Test message for close\n",
    "parent_conn.close()"
   ],
   "id": "70866daa137ee8ab",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test message for close\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. start() 与 join() 方法\n",
    "\n",
    "- `start()` 用于启动进程，调用后目标函数会在新的子进程中运行；\n",
    "- `join()` 用于等待子进程结束，确保主进程在子进程结束后再继续执行。"
   ],
   "id": "d2b4983cc4af3d98"
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T07:56:04.779343Z",
     "start_time": "2025-03-04T07:56:04.695005Z"
    }
   },
   "source": [
    "from multiprocessing import Process\n",
    "\n",
    "def worker():\n",
    "    print(\"Worker process started.\")\n",
    "\n",
    "p = Process(target=worker)\n",
    "p.start()  # 启动子进程\n",
    "p.join()   # 等待子进程结束\n",
    "print(\"Worker process has finished.\")"
   ],
   "id": "c1db0c91da51cb23",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Worker process has finished.\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. 综合示例\n",
    "\n",
    "下面的综合示例展示了如何使用 `Pipe` 和 `Process` 实现父子进程之间的通信："
   ],
   "id": "ee12ba00f289092a"
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T08:03:46.476300Z",
     "start_time": "2025-03-04T08:01:58.322996Z"
    }
   },
   "source": [
    "# 由于Jupyter Notebook的环境原因，以下代码只能在python文件中运行\n",
    "\n",
    "from multiprocessing import Pipe, Process\n",
    "\n",
    "# 创建管道，返回两个连接对象：parent_conn 用于父进程，child_conn 用于子进程\n",
    "parent_conn, child_conn = Pipe()\n",
    "\n",
    "def child_process(conn):\n",
    "    # 子进程通过管道发送消息\n",
    "    conn.send(\"Hello from child process!\")\n",
    "    # 发送完毕后关闭连接\n",
    "    conn.close()\n",
    "if __name__ == '__main__': # Windows中必须加这一行\n",
    "    # 创建并启动子进程，传入child_conn作为参数\n",
    "    p = Process(target=child_process, args=(child_conn,))\n",
    "    p.start()\n",
    "\n",
    "    # 父进程通过parent_conn接收子进程发送的消息，并打印输出\n",
    "    print(parent_conn.recv())  # 输出: Hello from child process!\n",
    "\n",
    "    # 等待子进程结束\n",
    "    p.join()"
   ],
   "id": "ffd1de67b4da39e8",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "version": "3.x"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
